<template>

    <button @click="changeNode">修改节点颜色事件</button>

    <h2>添加自定义形状</h2>

    <div id="chart-diagram" style="background-color: whitesmoke; width: 1000px; height: 1000px;">
    </div>


</template>

<script setup>
    import {onMounted} from 'vue';
    import go from "gojs";

    let myDiagram = null;


    const changeNode = () => {
        var data = myDiagram.model.findNodeDataForKey("Delta");
        // 这个方式可以更新
        if (data !== null) myDiagram.model.setDataProperty(data, "color", "red");
    }

    const fixArr = [
        {from: "p1", to: "p5", color: "blue", thick: 2, category: "judgeLink11"}
    ]

    const judgeArr = [
        {from: "p1", to: "p2", category: 'judgeLink', color: "blue", thick: 2},
        {from: "p3", to: "p4", category: 'judgeLink', color: "blue", thick: 2},
        {from: "p5", to: "p6", category: 'judgeLink', color: "blue", thick: 2},
        {from: "p7", to: "p8", category: 'judgeLink', color: "blue", thick: 2}
    ]

    const startAnimation = () => {
        var animation = new go.Animation();
        animation.easing = go.Animation.EaseLinear;
        myDiagram.links.each(function (link) {

            if (link.data.category == "judgeLink")
                animation.add(link.findObject("PIPE1"), "strokeDashOffset", 70, 0)
        });
        animation.runCount = Infinity;
        animation.start();
    }

    const getPorts = () => {
        return [    // for each node specify the location using Point values
            {key: "p1", category: "portNodeStyle", text: '1', color: "lightblue", loc: new go.Point(300, 80)},
            {key: "p2", category: "portNodeStyle", text: '2', color: "pink", loc: new go.Point(300, 150)},
            {key: "p3", category: "portNodeStyle", text: '3', color: "pink", loc: new go.Point(420, 150)},
            {key: "p4", category: "portNodeStyle", text: '4', color: "pink", loc: new go.Point(550, 150)},
            {key: "p5", category: "portNodeStyle", text: '5', color: "pink", loc: new go.Point(660, 150)},
            {key: "p6", category: "portNodeStyle", text: '6', color: "pink", loc: new go.Point(770, 150)},
            {key: "p7", category: "portNodeStyle", text: '7', color: "pink", loc: new go.Point(770, 80)},
            {key: "p8", category: "portNodeStyle", text: '8', color: "pink", loc: new go.Point(770, -15)},
            {key: "p9", category: "portNodeStyle", text: '9', color: "pink", loc: new go.Point(770, -125)}];
    }

    const getOkLinkTemplate = ($) => {
        return $(go.Link,

            $(go.Shape, {isPanelMain: true, stroke: "red", strokeWidth: 1}),
            $(go.Shape, {
                isPanelMain: true,
                stroke: "crimson",
                strokeWidth: 5,
                name: "PIPE1",
                strokeDashArray: [5, 65]
            }),
        );
    }

    const init = () => {

        const $ = go.GraphObject.make

        myDiagram = new go.Diagram("chart-diagram", {
            'grid.visible': true, // 显示网格
            'clickCreatingTool.archetypeNodeData': {category: 'Normal', text: '新节点', notice: ''}, // 双击新建节点(可以写入节点的默认信息)；；
            'toolManager.hoverDelay': 100, // tooltip提示显示延时
            'toolManager.toolTipDuration': 10000, // tooltip持续显示时间
            allowMove: true, // 允许拖动画板
            allowDragOut: true, // 允许拖拽节点
            allowDelete: true, // 允许删除节点
            allowCopy: true, // 允许复制节点
            allowClipboard: true, // 允许粘贴节点

        });

        myDiagram.linkTemplateMap.add("judgeLink", getOkLinkTemplate($));


        myDiagram.nodeTemplate =
            $(go.Node, "Auto",
                {locationSpot: go.Spot.Center},
                $(go.Shape, "RoundedRectangle",
                    { // 自定义highlight
                        fill: "yellow", stroke: "orange", strokeWidth: 2
                    },
                    new go.Binding("fill", "highlight", function (v) {
                        return v ? "pink" : "lightblue";
                    }),
                    new go.Binding("stroke", "bSearch", function (v) {
                        return v ? "red" : "blue";
                    }),
                    new go.Binding("strokeWidth", "bSearch", function (v) {
                        return v ? 3 : 0;
                    })
                ),
                $(go.TextBlock,
                    {margin: 5},
                    new go.Binding("text", "key")),
            );


        let nodeDataArray = getPorts();
        //let linkDataArray = [];

        let linkDataArray = fixArr;
        myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);


        judgeArr.map((r) => {
            myDiagram.model.addLinkData(r)
        })

        //
        startAnimation();


    };

    onMounted(() => {
        init();
    });


</script>

<style scoped>

</style>